package com.snake.springboot.custom.service.impl.base;

import com.snake.springboot.custom.api.model.base.CustMainAccountModel;
import com.snake.springboot.custom.api.request.base.CustomAdminReq;
import com.snake.springboot.custom.api.request.base.CustomReq;
import com.snake.springboot.custom.api.request.base.MainAccPwdReq;
import com.snake.springboot.custom.api.service.base.CustMainAccountService;
import com.snake.springboot.custom.dao.mybatis.entity.base.CustMainAccountDO;
import com.snake.springboot.custom.dao.mybatis.mappers.base.CustMainAccountMapper;
import com.snake.springboot.custom.service.assemble.base.CustMainAccAssemble;
import org.snake.common.exception.OperationException;
import org.snake.common.security.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 商户主账号业务实现Service
 * 类{@code CustMainAccountServiceImpl} </br>
 * 创建时间：2025/02/08 09:35
 * @author yangxt
 * @see
 */
@Service
public class CustMainAccountServiceImpl implements CustMainAccountService{

    @Autowired
    private CustMainAccountMapper custMainAccountMapper;

    @Override
    public void modifyCustomMainAccount(CustMainAccountModel model) {
        CustMainAccountDO accountDO =CustMainAccAssemble.mainAccDOAss(model);
        custMainAccountMapper.update(accountDO);
    }

    @Override
    public void updateAcc(CustomAdminReq model) {
        CustMainAccountDO accountDO = new CustMainAccountDO();
        accountDO.setCustomId(model.getCustomId());
        accountDO.setAccountName(model.getMainAccount().getAccountName());
        custMainAccountMapper.updateAccName(accountDO);
    }

    @Override
    public void updatePwdByMe(MainAccPwdReq pwdReq) {
        CustMainAccountDO accountDO=new CustMainAccountDO();
        accountDO.setCustomId(pwdReq.getCustomId());
        accountDO.setPassword(MD5Util.encrypt(pwdReq.getPassword()));
        custMainAccountMapper.updatePassword(accountDO);


    }

    @Override
    public void updatePwdByAdmin(MainAccPwdReq pwdReq) {
        CustMainAccountDO accountDO=new CustMainAccountDO();
        accountDO.setCustomId(pwdReq.getCustomId());
        accountDO.setPassword(MD5Util.encrypt(pwdReq.getPassword()));
        custMainAccountMapper.updatePassword(accountDO);
    }

    @Override
    public void updateState(CustMainAccountModel model) {
        CustMainAccountDO accountDO =CustMainAccAssemble.mainAccDOAss(model);
        custMainAccountMapper.update(accountDO);
    }

    @Override
    public void addMainAccount(CustomReq customReq) {
        CustMainAccountDO accountDO =CustMainAccAssemble.mainAccDOAss(customReq);
        CustMainAccountDO mainAccountDO=custMainAccountMapper.queryAcc(accountDO);
        OperationException.checkIsTrue(mainAccountDO==null,"当前账号已被注册");
        custMainAccountMapper.insert(accountDO);
    }

    @Override
    public CustMainAccountModel queryByAccountId(String id) {
        CustMainAccountDO accountDO = custMainAccountMapper.queryById(id);
        return CustMainAccAssemble.mainAccModelAss(accountDO);
    }

}
